<?php
  require("../configuracion.php");
   $idSysSection = $sectionSegmentacion;
  require("../application-top.php");
  require "segmento-info.php";
  require("segmento-admin-functions.php");
//  $_url = "&_url=" . $_SERVER['PHP_SELF'] . "?" . $_SERVER["QUERY_STRING"] . "&action=bajar";

  if ($action == "bajar" ){
    $_url = "?" . $_SERVER["QUERY_STRING"];
    require("segmento-export.php");
  }
  else {
    if ($_SERVER["QUERY_STRING"] == '') $_url = "?" . "action=bajar";
    else $_url = "?" . $_SERVER["QUERY_STRING"] . "&action=bajar";
  }

  $i = strpos($_url,"&id=");
  if ($i > 0 ) $_url = substr ($_url,0,($i));

?>
<html>
<head>
  <title><?= $instanciaECRM ?></title>
  <? if ($action == "bajar" ):?>
    <META HTTP-EQUIV="Refresh" CONTENT="0;URL=<?= $url ?>">
  <? endif ?>
  <link rel="stylesheet" href="../styles/style.css" type="text/css">
</head>
<body leftmargin="0" class="mainBackground" topmargin="0" marginwidth="0" marginheight="0">
<form name="forma">
<table width="100%" border="0" cellpadding="0" cellspacing="0">
  <tr><td><img src="../images/headerBar.gif" width="590" height="60" alt=""></td></tr>
  <tr>
    <td><table width="800" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td bgcolor="#FFFFFF" valign="top" height="400">

<table border="0" cellpadding="5" cellspacing="0" width="750">
  <tr><td colspan="2"><table border="0" cellpadding="0" cellspacing="0" width="100%">
    <tr><td colspan="2"><img src="../images/spacer.gif" height="10" width="1" alt=""></td></tr><tr><td><strong class="pageTitle">Segmentaci&oacute;n</strong></td><td align="right"><a href="http://www.cacto.com/" target="_blank"><img src="../images/trademarkCacto.gif" alt="Cacto" border="0"></a><br>
<strong class="systemTitle"><?= $instanciaECRM ?></strong><br>Perfil <strong><?= $PerfilUsuario ?></strong></td></tr>
  </table></td></tr>
  <tr>
  <td valign="top" width="100%">
    <table border="0" cellpadding="0" cellspacing="0" width="100%">
      <tr><td align="right">
          <table border="0" cellpadding="2" cellspacing="0"  width="100%">
        <tr>
                  <td>
                     <?= RegistrosLista($pag,$n_row,$total_registros) ?>
                  </td>
                  <form method="GET" action="<?= $_SERVER['PHP_SELF'] ?>">
                  <td align="right">
                    <input type="text" name="buscar" value="<?= $buscar ?>">
                    <input type="hidden" name="action" value="buscar">
                    <input type="hidden" name="n_row" value="<?= $n_row ?>">
                  </td>
                  <td  width="5"><INPUT TYPE=IMAGE NAME="imagen" SRC="../images/icBuscar.gif"></td>
                  </form>
                 </tr>
      </table>
      </td></tr>
      <tr><td><img src="../images/spacer.gif" height="5" width="1" alt=""></td></tr>
      <tr><td colspan="2">
        <table border="0" cellpadding="5" cellspacing="1" width="100%" class="secTableBG">
          <tr class="answerCellBG"><td colspan="6" align="right">
            <table border="0" cellpadding="0" cellspacing="0" width="100%">
            
              <tr>
                               <form method="GET" action="<?= $_SERVER['PHP_SELF'] ?>">
                <td>Mostrar
                                  <select name="n_row">
<?
                                $registros[0] = "10";
                                $registros[1] = "25";
                                $registros[2] = "50";
                                $registros[3] = "100";
                                foreach($registros as $n){
                                  if ($n == $n_row){
                                    print "<option value=\"" . $n . "\" selected>" . $n . "</option>\n";
                                  }
                                  else {
                                    print "<option value=\"" . $n . "\">" . $n . "</option>\n";
                                  }
                                }
?>
                                  </select>
                                 <INPUT TYPE=IMAGE NAME="imagen" SRC="../images/icArrowGo.gif">
                                 <input type="hidden" name="buscar" value="<?= $buscar ?>">
                                 <input type="hidden" name="order" value="<?= $order ?>">
                                </td>
                                 </form>
                <td align="right"><? paginas($pag,$n_paginas,$liga1) ?></td>
              </tr>
            </table>
          </td></tr>
          <tr>
            <td><table border="0" cellpadding="2" cellspacing="0">
              <tr>
                <td class="secTableTitle"><strong>Segmento</strong></td>
                <? $liga = $_SERVER['PHP_SELF'] . "?order=asc&col_order=st_Segmento"; ?>
                <td><a href="<?= $liga . $liga2 ?>"><img src="../images/icSortAsc.gif" height="12" width="13" alt="Orden Ascendente" border="0"></a></td>
                <? $liga = $_SERVER['PHP_SELF'] . "?order=desc&col_order=st_Segmento"; ?>
                <td><a href="<?= $liga . $liga2 ?>"><img src="../images/icSortDesc.gif" height="12" width="13" alt="Orden Descendente" border="0"></a></td>
              </tr>
            </table></td>
            <td class="secTableTitle"><strong>Descripci&oacute;n</strong></td></td>
            <td class="secTableTitle"><strong><?= $Audiencias1 ?>&nbsp;en&nbsp;Segmento</strong></td>
            <td class="secTableTitle"><strong>Fecha&nbsp;Creaci&oacute;n</strong></td></td>
            <td class="secTableTitle" align="center"><strong>Extraer&nbsp;tabla</strong></td>
          </tr>
          
<?php
  $_class="conceptCellBG";
  $q = Paginar($pag,$n_row,$order,$total_registros,$buscar,$campo_order);
  $result = mssql_query ($q);
  while ($row = mssql_fetch_object($result)):
  if (($row->dia <> "") and ($row->mes <> "")) {
		  $dia = $row->dia;
		  if (strlen($dia)<2) { $dia = "0".$dia;  }
		  $mes = $row->mes;
		  if (strlen($mes)<2) { $mes = "0".$mes;  }
  		$dt_FechaRegistro = $dia . "/" . $mes . "/" . $row->ano;
		}
  else $dt_FechaRegistro = "";

  //calcular en tiempo real el numero de registros para cada segmento para aprobechar mas el CRM
  $q = "select count(*) as NumReg from (" . html_entity_decode(urldecode($row->tx_Query)) . ") as t1";
  $result2 = @mssql_query ($q);
  $row2 = @mssql_fetch_object($result2);
  $rn = $row2->NumReg;
?>
          <tr class="<?= $_class ?>">
            <td><a href="segmento-detalle.php?id=<? print $row->id_Segmento . "&nr=" . $rn ?>"><?= html_entity_decode(urldecode($row->st_Segmento)) ?></a></td>
            <td><?= html_entity_decode(urldecode($row->st_SegmentoDescripcion)) ?></td>
            <td align="right"><?= number_format($rn) ?></td>
            <td align="right"><?= $dt_FechaRegistro ?></td>
            <td align="center"><a href="<?= $_SERVER['PHP_SELF'] ?><?= $_url ?>&id=<?= $row->id_Segmento ?>"><img src="../images/icDownloadXLS.gif" height="24" width="24" alt="Extraer a MS Excel" border="0"></a></td>

            
          </tr>
<?php
  flush();  //tratar de enviar lo que va de la salida al cliente. Depende del webserver y del browser esta caracteristica

      if ($_class == "conceptCellBG"){
         $_class = "answerCellBG";
      }
      else {
         $_class = "conceptCellBG";
      }
  endwhile;
?>
          <tr class="answerCellBG"><td colspan="6" align="right"><? paginas($pag,$n_paginas,$liga1) ?></td></tr>

        </table>
      </td></tr>
      <tr><td><img src="../images/spacer.gif" height="10" width="1" alt=""></td></tr>
      <tr><td>
<?  if (Permisos($sectionSegmentacion)):?>
        <table border="0" cellpadding="5" cellspacing="1" class="mainTableBG">
          <tr class="answerCellBG">
            <td><img src="../images/icArrowGo.gif" height="13" width="13" alt=""></td>

            <td><a href="segmento-nuevo.php">Crear nueva segmentaci&oacute;n</a></td>
          </tr>
        </table>
<?  endif; ?>
      </td></tr>
    </table>
  </td>
  </tr>
</table>

          </td>
          <!-- <td width="36" background="../images/f_int.jpg"><img src="../images/f_int.jpg" width="36" height="126"></td> -->
        </tr>
      </table></td>
  </tr>
</table>
<!-- <table width="100%" border="0" cellpadding="0" cellspacing="0" background="../images/fotterint.jpg">
  <tr>
    <td><table width="800" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td><img src="../images/f_int_new.jpg" height="32" width="800" alt=""></td>
        </tr>
      </table></td>
  </tr>
</table> -->


</form>
<br>
<br>
<br>
<table width="750" border="0" cellpadding="5" cellspacing="0">
  <tr>
    <td align="right"><span class="copyright"><?= $copyright ?></span></td>
  </tr>
</table>
</body>
</html>
